package net.onecloud.onecloud.account;


import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


import net.onecloud.onecloud.R;
import net.onecloud.onecloud.base.BaseAppCompatActivity;

import net.onecloud.onecloud.model.ResultPost;
import net.onecloud.onecloud.model.ResultUser;
import net.onecloud.onecloud.model.User;
import net.onecloud.onecloud.network.NetWork;

import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class RegisterActivity extends BaseAppCompatActivity {

    private EditText mUsername;
    private EditText mPassword;
    private EditText mRePassword;
    private Button mBTRegister;

    private Observer<ResultUser> registerObserver = new Observer<ResultUser>() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {

        }

        @Override
        public void onNext(ResultUser resultUser) {
            if (resultUser == null) {
                showHit("注册失败");
                return;
            }
            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putParcelable("user", resultUser.getData());
            intent.putExtras(bundle);
            RegisterActivity.this.setResult(RESULT_OK, intent);
            showHitMustFinish("注册成功");
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        setToolBarTitle("注册");
        HideLeftButton(false);
        initViews();
        initListener();
        initData();
    }

    private void initData() {
    }

    private void initListener() {
        mBTRegister.setOnClickListener(this);
    }

    private void initViews() {
        mUsername = (EditText) findViewById(R.id.m_username);
        mPassword = (EditText) findViewById(R.id.m_password);
        mRePassword = (EditText) findViewById(R.id.m_repassword);
        mBTRegister = (Button) findViewById(R.id.m_bt_register);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.m_bt_register: {
                startRegister();
                break;
            }
        }
    }

    private void startRegister() {
        final User user = getRegisterInfo();
        if (user != null) {
            NetWork.getAccountService()
                    .checkUserName(user.getUsername())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .flatMap(new Func1<ResultUser, Observable<ResultUser>>() {
                        @Override
                        public Observable<ResultUser> call(ResultUser resultUser) {
                            if (resultUser != null) {
                                if (resultUser.getCode() == 200 && resultUser.getSuccess() == 1) {
                                    return NetWork.getAccountService().register(user).subscribeOn(Schedulers.io());
                                }
                                showHit("该账号已被注册");
                            }
                            return null;
                        }
                    })
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(registerObserver);

        }
    }

    public User getRegisterInfo() {
        String username = mUsername.getText().toString();
        String password = mPassword.getText().toString();
        String rePassword = mRePassword.getText().toString();
        String info = "";
        if (TextUtils.isEmpty(username)) {
            info = "用户名不能为空";
        } else if (TextUtils.isEmpty(password)) {
            info = "密码不能为空";
        } else if (TextUtils.isEmpty(rePassword)) {
            info = "确认密码不能为空";
        } else if (!rePassword.equals(password)) {
            info = "两次密码不一致";
        }
        if (info.equals("")) {
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            return user;
        }
        showHit(info);
        return null;


    }
}
